<?php

namespace App\Repositories\Address;

use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\Address\RegionRepository;
use App\Models\Address\Region;
use App\Validators\Address\RegionValidator;

/**
 * Class RegionRepositoryEloquent
 * @package namespace App\Repositories\Address;
 */
class RegionRepositoryEloquent extends BaseRepository implements RegionRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Region::class;
    }

    /**
     * ID 转为 名称
     * @param int $id
     * @param int $type
     * @return mixed
     */
    public function idToString(int $id, int $type){
        $nameArray = $this->findWhere(['region_id'=>$id,'region_type'=>$type],['region_name']);
        return $nameArray[0]['region_name'];
    }

    /**
     * 名称转为ID
     * @param string $name
     * @param int $type
     * @return mixed
     */
    public function stringToId(string $name, int $type){
        $name = str_replace(array('省','市','区','县','自治区','自治州','自治镇','自治县','特别行政区'),'',$name);
        $nameArray = $this->findWhere(['region_name'=>$name,'region_type'=>$type],['region_name']);
        return $nameArray[0]['region_id'];
    }


    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }
}
